﻿using System;
using System.Linq;
using BehavioralPatterns.Visitor.DataProcessors;
using RealisticDependencies;

namespace BehavioralPatterns.Visitor.Visitors {
    public class SaleDataVisitor : IVisitor<SalesReport> {
        private readonly IApplicationLogger _logger;

        public SaleDataVisitor(IApplicationLogger logger) {
            _logger = logger;
        }

        /// <summary>
        /// Here the Florist Co-Op wants to be emailed when we run the Sales Report
        /// The FloristDataProcessor doesn't know anything other than how to generate
        /// the data it already works with, and a method enabling it to be "visited."
        /// </summary>
        /// <param name="processor"></param>
        /// <returns></returns>
        public SalesReport Visit(FloristDataProcessor processor) {
            Console.ForegroundColor = ConsoleColor.Magenta;
            _logger.LogInfo("Visiting Florist for Sales Report");
            var sales = processor.GetDailyOrderAmounts();
            Console.ResetColor();
            return new SalesReport(_logger, DateTime.UtcNow, sales.Sum());
        }

        public SalesReport Visit(BakeryDataProcessor processor) {
            Console.ForegroundColor = ConsoleColor.Magenta;
            _logger.LogInfo("Visiting Bakery for Sales Report");
            var sales = processor.GetDailyOrderAmounts();
            Console.ResetColor();
            return new SalesReport(_logger, DateTime.UtcNow, sales.Sum());
        }

        public SalesReport Visit(FarmerDataProcessor processor) {
            Console.ForegroundColor = ConsoleColor.Magenta;
            _logger.LogInfo("Visiting Farmer for Sales Report");
            var sales = processor.GetDailyOrderAmounts();
            Console.ResetColor();
            return new SalesReport(_logger, DateTime.UtcNow, sales.Sum());
        }
    }

    public class SalesReport : Report {
        public SalesReport(IApplicationLogger logger, DateTime date, decimal totalSales) : base(logger) {
            CreatedBy = "SalesDataRobot";
            CreatedOn = date;
            TotalSales = totalSales;
        }

        public decimal TotalSales { get; }

        public override void Print() {
            _logger.LogInfo($"=== 💸 Sales Data for {CreatedOn} 💸 ===", ConsoleColor.Green);
            _logger.LogInfo($"=== Generated By: {CreatedBy} ===", ConsoleColor.Green);
            _logger.LogInfo($"- Total Sales Today: ${TotalSales}", ConsoleColor.Green);
            _logger.LogInfo("\n");
        }
    }
}
